package com.poisonh.unifiedauth.ui;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.lzy.okhttputils.model.HttpParams;
import com.poisonh.unifiedauth.R;
import com.poisonh.unifiedauth.utils.ConstantUitls;
import com.poisonh.unifiedauth.utils.MyHttpsUtils;
import com.poisonh.unifiedauth.utils.RegexUtils;
import com.poisonh.unifiedauth.utils.ToastUtils;

/**
 * 输入邮箱地址
 * Created by PoisonH on 2016/6/28.
 */
public class InputEmailActivity extends BaseActivity implements View.OnClickListener
{
    private Button mBtnNext;
    private EditText mMetInputEmail;
    private String mStrEmail;

    @Override
    protected void loadContentView(Bundle savedInstanceState)
    {
        setContentView(R.layout.activity_inputemail);
        initTitleView();
        setTitleTxt("绑定邮箱");
        initView();
    }

    @Override
    protected void initView()
    {
        mRightBtn.setVisibility(View.GONE);
        mLeftBtn.setOnClickListener(this);
        mBtnNext = (Button) findViewById(R.id.btn_next);
        mMetInputEmail = (EditText) findViewById(R.id.bindemail_input_email);
        mMetInputEmail.addTextChangedListener(mEmailTextWatcher);
        if (TextUtils.isEmpty(this.getIntent().getStringExtra("CHANGE_EMAIL")) && mStrUserEmail != null)
        {
            mMetInputEmail.setText(mStrUserEmail);
        }
        mBtnNext.setOnClickListener(this);
    }

    @Override
    public void onClick(View v)
    {
        switch (v.getId())
        {
            case R.id.ib_left_btn:
                finish();
                break;
            case R.id.btn_next:
                mStrEmail = mMetInputEmail.getText().toString();
                mBtnNext.setEnabled(false);
                if (mStrEmail.equals(mStrUserEmail))
                {
                    ToastUtils.showToast(InputEmailActivity.this, "请输入新的邮箱地址");
                    mBtnNext.setEnabled(true);
                } else
                {
                    if (!RegexUtils.isEmail(mStrEmail))
                    {
                        ToastUtils.showToast(InputEmailActivity.this, "请输入正确的邮箱地址");
                        mBtnNext.setEnabled(true);
                    } else
                    {

                        verifyEmail(mStrEmail);
                    }
                }
                break;
        }
    }

    /**
     * 验证邮箱是否已绑定
     *
     * @param email
     */
    private void verifyEmail(String email)
    {
        HttpParams params = new HttpParams();
        params.put("id", mStrUserId);
        params.put("email", email);
        params.put("token", mStrUserToken);
        MyHttpsUtils.post(ConstantUitls.REQUEDT_URL + ConstantUitls.VERIFY_NEW_EMAIL, params, new MyHttpsUtils.ResultCallback()
        {
            @Override
            public void onSuccess(String response)
            {
                mBtnNext.setEnabled(true);
                Intent mIntent = new Intent(InputEmailActivity.this, BindEmailActivity.class);
                mIntent.putExtra("EMAIL", mStrEmail);
                startActivity(mIntent);
                finish();

            }

            @Override
            public void onFailure(String failReason)
            {
                mBtnNext.setEnabled(true);
                ToastUtils.showToast(InputEmailActivity.this, failReason);
            }
        });
    }

    TextWatcher mEmailTextWatcher = new TextWatcher()
    {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after)
        {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count)
        {

        }

        @Override
        public void afterTextChanged(Editable s)
        {
            if (RegexUtils.isEmail(s.toString()))
            {
                mBtnNext.setEnabled(true);
                mBtnNext.setBackground(getResources().getDrawable(R.drawable.shape_settingmyinfo_btn_bg));
            } else
            {
                mBtnNext.setEnabled(false);
                mBtnNext.setBackground(getResources().getDrawable(R.drawable.shape_settingmyinfo_btn_bg_));
            }
        }
    };

}
